<template>
  <div>
    <div>
      <el-button type="success" plain @click="addimg" style="margin-left: 90px"
        >上传图片</el-button
      >
    </div>
    <el-table :data="urldata" style="width: 100%" v-loading="loading">
      <el-table-column label="轮播图" width="280px" align="center">
        <template slot-scope="scope">
          <!-- <span>{{scope.row.id}}</span> -->
          <img
            :src="imgsrcip + scope.row.imagpath"
            alt="轮播图"
            style="width: 200px; height: 100px"
          />
          <!-- <el-image
            style="width: 100px; height: 100px"
            :src="'http://192.168.1.113:80' + scope.imagpath"
          ></el-image> -->
        </template>
      </el-table-column>
      <el-table-column label="创建时间" width="280px" align="center">
        <template slot-scope="scope"
          ><span>{{ scope.row.creattime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="280px">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="danger"
            @click="delImg(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      title="添加轮播图"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <el-upload
        class="avatar-uploader"
        :action="ITEMS_IMG_UPLOAD_HOMEIMG"
        :show-file-list="false"
        :on-success="handleAvatarSuccess"
        :headers="headers"
      >
        <img v-if="imageUrl" :src="imgsrcip + imageUrl" class="avatar" />
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="refr">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

  
  <script>
import {
  ITEMS_IMG_UPLOAD_HOMEIMG,
  findHomeImg,
  HomeImg,
  imgsrcip,
  homeurl,
} from "@/api/apis";
export default {
  data() {
    return {
      urldata: [],
      dialogVisible: false,
      imageUrl: "",
      ITEMS_IMG_UPLOAD_HOMEIMG: "",
      loading: false,
      imgsrcip: "",
    };
  },
  methods: {
    addimg(index, row) {
      this.dialogVisible = true;
      // console.log(index, row);
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
    handleAvatarSuccess(res, file) {
      // this.imageUrl = URL.createObjectURL(file.raw);
      // console.log(res);
      this.imageUrl = res.data;
      // console.log(this.imageUrl);
    },
    delImg(index, row) {
      // console.log(index, row);
      this.$confirm("此操作将永久删除该图片, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          HomeImg(row.id).then((res) => {
            // console.log(res)
            if (res.data.code == 200) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.refresh();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //请求图片
    refresh() {
      findHomeImg().then((res) => {
        // console.log(res);
        this.urldata = res.data.data;
        // console.log(this.urldata);
        this.loading = false;
      });
    },
    refr() {
      console.log(this.imageUrl);
      homeurl(this.imageUrl).then((res) => {
        // console.log("测试");
        // console.log(res);
        if (res.data.code == 200) {
          this.dialogVisible = false;
          this.refresh();
          this.imageUrl=''
        }
      });
    },
  },
  created() {
    this.loading = true;
    this.ITEMS_IMG_UPLOAD_HOMEIMG = ITEMS_IMG_UPLOAD_HOMEIMG;
    this.imgsrcip = imgsrcip;
    // console.log(this.imgsrcip)
    this.refresh();
  },
  computed: {
    headers() {
      return {
        username: sessionStorage.getItem("username"),
      };
    },
  },
};
</script>
  <style scoped>
.block {
  margin-right: 10px;
}
div:first-child {
  margin-bottom: 5px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader {
  border: 1px dashed #8c939d;
  width: 90px;
  height: 90px;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 90px;
  height: 90px;
  line-height: 90px;
  text-align: center;
}
.avatar {
  width: 90px;
  height: 90px;
  display: block;
}
</style>